/**
 @description 地图功能 
 @author ffpan
 @requires  googlemap api  (http://ditu.google.cn/maps?file=api&amp;v=2.x&amp;key=ABQIAAAAWlfiFVYKHX8tCx74bFRnfhRpp30BDXp-1xIerK7XpUs0bSzu7xSVrUD63jB2YX5cVd1HtH12nrekzg&hl=zh-CN)
 @version 1.0
*/

MyGoogleMap = function(){
	this.initialize();
}

MyGoogleMap.prototype = {
	
	/** 初始化地图 **/
	initialize : function(){
		/**初始化参数 **/
		this.mapDiv = document.getElementById("map_canvas");
		
		if (GBrowserIsCompatible()) {
			this.map = new GMap2(this.mapDiv);
			this.map.setCenter(new GLatLng(31.979, 120.894), 13);
			this.geocoder = new GClientGeocoder();
			//this.map.addControl(new GMapTypeControl());
			//this.map.addControl(new GLargeMapControl());

			this.map.addControl(new GSmallMapControl());
		}
	},
	
	/** 显示地图 **/
	drawMap : function(){
	
	},

	/** 生成icon *****/
	getIcon : function(){
		var icon = new GIcon();
		icon.image = "mm.png";
		icon.iconSize = new GSize(48, 40);
		icon.iconAnchor  = new GPoint(5,5);
		return icon;
	},
	
	/** 根据地名返回经纬度 **/
	getPointer : function(address){
		var  _this = this;
		if (this.geocoder) {
			this.geocoder.getLatLng( address , function(point) {
				if(!point){point="unknown";}
				//_this.postData(point);
				document.title = "lat:"+point.y+"--lng:"+point.x;
				_this.map.setCenter(point, 16);

				var hicon = _this.getIcon();
				var marker = new GMarker(point , {icon : hicon , dragCrossMove:true , title:"hello" , draggable:true});
				// _this.map.addOverlay(marker);

				var new_marker=new KMarker(new GLatLng(Number(point.y) ,  Number(point.x)), "testtips", "48套" ,  {"index": 1});
				_this.map.addOverlay(new_marker);

		});
      }
	},
	
	/** 将对应的名称和经纬度提交到后台页面 **/
	postData : function(point){
		var data = {
			name : $("#name").attr("value"), 
			address : $("#address").attr("value"),
			pLat : point.y,
			pLng : point.x
		}
		var url = "map.php";
		$.get(url , data, function(txt){
			alert("Data Loaded: " + txt);
		}); 
	}
}

/*****************新增KMarker*****************/
var _index = 1;
var KMarker = function(latlng, tip, markerContent, clickParamer){
	this._latlng = latlng;
	this._tip = tip;
	this._markerContent = markerContent;
	this._clickParamer = clickParamer;
}

KMarker.prototype = new GOverlay();

KMarker.prototype.initialize = function(map_) {
    var markerHtml = document.createElement('div');
    markerHtml.setAttribute("index", this._clickParamer["index"]);
    markerHtml.innerHTML = this._markerContent;
	markerHtml.className = "marker";
    map_.getPane(G_MAP_MARKER_PANE).appendChild(markerHtml);
    var markerTip = document.createElement("div");
   /* markerTip.innerHTML = this._tip;
    markerTip.style.position = "absolute";
    markerTip.style.display = "none";
	markerTip.style.color = "#9e93de";*/
    this._map = map_;
    //map_.getPane(G_MAP_MARKER_PANE).appendChild(markerTip);
 
    GEvent.addDomListener(markerHtml, "mouseover", function() {
		document.title += "0";
		//markerHtml.className  = "marker marker_selected";
      /*  if (markerTip.style.display == "none") {
				markerTip.style.display = "";
            //_mapPage.viewHotelMarker(markerHtml.getAttribute("index"));
        } else {
			markerTip.style.display = "none";
            //_mapPage.hiddenHotelMarker(markerHtml.getAttribute("index"));
        }*/
    });
   /* GEvent.addDomListener(markerTip, "click", function() {
        if (markerTip.style.display == "none") {
            //_mapPage.viewHotelMarker(markerHtml.getAttribute("index"));
        } else {
            //_mapPage.hiddenHotelMarker(markerHtml.getAttribute("index"));
        }
 
    });
    GEvent.addDomListener(markerHtml, "mouseover", function() {
        _index++;
        markerTip.style.zIndex = _index;
        markerHtml.style.zIndex = _index;
    });
    GEvent.addDomListener(markerTip, "mouseover", function() {
        _index++;
        markerTip.style.zIndex = _index;
        markerHtml.style.zIndex = _index;
 
    });*/
    this._markerHtml = markerHtml;
    this._markerTip = markerTip;
};
KMarker.prototype.remove = function() {
    this._markerHtml.parentNode.removeChild(this._markerHtml);
    this._markerTip.parentNode.removeChild(this._markerTip);
};
KMarker.prototype.copy = function() {
    return new (this._latlng,this._tip,this._markerContent,this._clickParamer);
};
KMarker.prototype.redraw = function(force) {
    if (force){
        var p = this._map.fromLatLngToDivPixel(this._latlng);
        this._markerHtml.style.left = (p.x) + "px";
        this._markerHtml.style.top =  (p.y)  + "px";
        this._markerTip.style.left = (p.x)  + "px";
        this._markerTip.style.top = (p.y)  + "px";
    }
};
 
KMarker.prototype.getMarkerHtml = function() {
    return this._markerHtml;
}
 
KMarker.prototype.getMarkerTip = function() {
    return this._markerTip;
}



















/*************************************************/

$(document).ready(function(){
	var newMap = new MyGoogleMap();
	var url = "map.php";
	$("#trigger").bind("click" , function(){
		var address = $("#address").attr("value");
		newMap.getPointer(address);
		
	});
}); 


window.unload = function(){
	GUnload();
}